import java.util.Iterator;
import java.util.function.Consumer;

public class LinkList {
    public static void main(String[] args) {
        SinglyLinkedlist singlyLinkedlist = new SinglyLinkedlist();
        singlyLinkedlist.addLast(1);
        singlyLinkedlist.addLast(2);
        singlyLinkedlist.addLast(3);
        singlyLinkedlist.addLast(4);

        singlyLinkedlist.addLast(4);

        singlyLinkedlist.insert(1,20);

//        singlyLinkedlist.removeFirst();
//        singlyLinkedlist.remove(2);
//        System.out.println(singlyLinkedlist.get(0));
//        System.out.println(singlyLinkedlist.get(2));
        singlyLinkedlist.loop2();


//
//        singlyLinkedlist.loop1(value -> System.out.println(value));
//
//        for(Integer i : singlyLinkedlist){
//            System.out.println(i);
//        }


    }


}

 class SinglyLinkedlist implements Iterable<Integer>{

     Node head = null;


     //遍历方法三
     @Override
     public Iterator<Integer> iterator() {
         return new Iterator<Integer>() {
             Node p = head;
             @Override
             public boolean hasNext() {
                 //判断是否有下一个
                 return p!=null;
             }

             @Override
             public Integer next() {
                 //返回当前元素，并判断下一位置元素
                 int v = p.value;
                 p = p.next;
                 return v;
             }
         };
     }

     private static class Node{
         int value;
         Node next;



         public  Node(int value,  Node next){
             this.value = value;
             this.next = next;
         }
     }

     public void addFirst(int value){
         //1.链表为空
//         head = new Node(value, null);
         //2.链表不为空,也可以为空
         head = new Node(value, head);
     }

//遍历:方法一
     public void loop(){
         Node p = head;
         while (p != null){
             System.out.println(p.value);
             p = p.next;
         }
     }

     //遍历:方法二
     public void loop1(Consumer<Integer> consumer){
         for (Node p =head;  p != null; p=p.next){
             consumer.accept(p.value);
         }
     }

     //递归
     public void loop2(){
         recur(head);
     }

     public void recur(Node cur){
         if (cur == null){
             return;
         }
         System.out.println(cur.value);
         recur(cur.next);
     }

     //寻找要插入元素位置
     private Node find(){
         if (head == null){
             return null;
         }
         Node p = head;
//         for (p = head;p.next !=null;){
//             p = p.next;
//         }

         while (p.next!=null){
             p = p.next;
         }
         return p;
     }

     //插入链表尾部
     public void addLast(int value){
         Node prev = find();
         if (prev == null){
             addFirst(value);
             return;
         }
         prev.next = new Node(value, null);
     }


     //根据索引找元素
     public Node findNode(int index){
         int i = 0;
         for (Node p = head;p != null;p = p.next,i++){
             if(i == index){
                 return p;
             }
         }
         return null;
     }

     public int get(int index){
         Node p =findNode(index);
         if (p == null){
             return 0;
         }
         return p.value;
     }


     //删除首个
     public void removeFirst(){
         if (head == null){
             return;
         }
         head = head.next;
     }

     //根据索引删除
     public void remove(int index){
         if(index==0){
             removeFirst();
             return;
         }
         Node p = findNode(index-1);
         if (p==null){
             return;
         }
         Node remove = p.next;
         if (remove==null){
             return;
         }
         p.next = remove.next;
     }

     //根据索引插入元素
     public void insert(int index,int value){
         Node p = findNode(index-1);
         if (p==null){
             return;
         }
         p.next = new Node(value,p.next);
     }


     //删除倒数节点
     public void removeN(Node head,int n){
         getLength(head);
     }

     public int getLength(Node head){
         int i = 0;
         while (head!=null){
             i++;
             head = head.next;
         }
         return i;
     }



}






